<h2>Why is this an issue?</h2>
<p>A cast is an <a
href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/implicit-and-explicit-conversions">explicit
conversion</a>, which is a way to tell the compiler the intent to convert from one type to another.</p>
<p>In Visual Basic, there are two explicit conversion operators:</p>
<pre>
Public Sub Method(Value As Object)
    Dim i As Integer
    i = DirectCast(Value, Integer)  ' Direct casting from object holding an integer type to Integer
    i = CType(Value, Integer)       ' Conversion from the underlying type to Integer
End Sub
</pre>
<p>In most cases, the compiler will be able to catch invalid casts between incompatible value types or reference types.</p>
<p>However, the compiler will not be able to detect invalid casts to <a
href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/interfaces/">interfaces</a>.</p>
<h3>What is the potential impact?</h3>
<p>Invalid casts will lead to unexpected behaviors or runtime errors such as <a
href="https://learn.microsoft.com/en-us/dotnet/api/system.invalidcastexception">InvalidCastException</a>.</p>
<h3>Exceptions</h3>
<p>No issue is reported if the interface has no implementing class in the assembly.</p>
<h2>How to fix it</h2>
<p>To prevent an <code>InvalidCastException</code> from raising during an explicit conversion, it is recommended to use the <a
href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator"><code>TryCast</code> operator</a>. When the
conversion is not possible, the <code>TryCast</code> operator returns <code>Nothing</code> and will never raise an exception.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Public Interface IMyInterface
End Interface

Public Class Implementer
    Implements IMyInterface
End Class

Public Class AnotherClass
End Class

Module Program
    Sub Main()
        Dim Another As New AnotherClass
        Dim x As IMyInterface = DirectCast(Another, IMyInterface)   ' Noncompliant: InvalidCastException is being thrown
    End Sub
End Module
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
Public Interface IMyInterface
End Interface

Public Class Implementer
    Implements IMyInterface
End Class

Public Class AnotherClass
End Class

Module Program
    Sub Main()
        Dim Another As New AnotherClass
        Dim x = TryCast(Another, IMyInterface)                      ' Compliant: but will always be Nothing
    End Sub
End Module
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/type-conversions">Type
  Conversions in Visual Basic</a>
    <ul>
      <li> <a
      href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/implicit-and-explicit-conversions">Implicit and Explicit Conversions in Visual Basic</a> </li>
      <li> <a
      href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/how-to-convert-an-object-to-another-type">How to: Convert an Object to Another Type in Visual Basic</a> </li>
    </ul>  </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/directcast-operator"><code>DirectCast</code>
  operator</a> </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/ctype-function"><code>CType</code> function</a>
  </li>
  <li> <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator"><code>TryCast</code> operator</a>
  </li>
  <li> CWE - <a href="https://cwe.mitre.org/data/definitions/588">CWE-588 - Attempt to Access Child of a Non-structure Pointer</a> </li>
  <li> CWE - <a href="https://cwe.mitre.org/data/definitions/704">CWE-704 - Incorrect Type Conversion or Cast</a> </li>
</ul>

